package com.flaskpy.web.eternity.auth.jwt.entiry;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class EyUserDetail implements UserDetails {

    private List<GrantedAuthority> authorities=new ArrayList<>();

    /**
     * 添加权限
     * @param names
     */
    public void addAuthority(String... names){
        authorities.addAll(Arrays.stream(names).map(p->new SimpleGrantedAuthority(p)).collect(Collectors.toList()));
    }

    /**
     * 添加角色
     * @param names
     */
    public void addRole(String... names){
        authorities.addAll(Arrays.stream(names).map(p->new SimpleGrantedAuthority("ROLE_"+p)).collect(Collectors.toList()));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
       return authorities;
    }



    @Override
    public String getPassword() {
        return "123456";
    }

    @Override
    public String getUsername() {
        return "sam";
    }

    @Override
    public boolean isAccountNonExpired() {
        //账号没过期
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        //没有锁定
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
